﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Workflow.Activities;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;
using System.Workflow.Runtime.Tracking;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace YakShaver.Tests
{
    /// <summary>
    /// Summary description for IntegrationTestBase
    /// </summary>
    [TestClass]
    public class IntegrationTestBase
    {

        protected static string sqlTrackingConnectionString;
        protected static string sqlPeristenceConnectionString;

        private static WorkflowRuntime workflowRuntime;
        private static ExternalDataExchangeService dataExchangeService;
        private bool createPersistenceDb = false;
        private bool createTrackingDb = false;

        protected static WorkflowRuntime WorkflowEngine
        {
            get
            {
                return workflowRuntime;
            }
        }

        protected ExternalDataExchangeService DataExchangeService
        {
            get
            {
                return dataExchangeService;
            }
            set
            {
                dataExchangeService = value;
            }
        }


        protected bool CreateTrackingDb
        {
            get { return createTrackingDb; }
        }
        protected bool CreatePersistenceDb
        {
            get { return createPersistenceDb; } 
        }

        internal IntegrationTestBase(bool createPersistenceDb, bool createTrackingDb)
        {
            this.createPersistenceDb = createPersistenceDb;
            this.createTrackingDb = createTrackingDb;
        }
        internal IntegrationTestBase()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext) {
            sqlTrackingConnectionString = "Data Source=.\\SQLExpress;Initial Catalog=WFTracking;integrated security=true;";
            sqlPeristenceConnectionString = "Data Source=.\\SQLExpress;Initial Catalog=WFPersistence;integrated security=true;";


            // recreate the tracking db


            // recreate the persistance db

            // initialize the runtime
            workflowRuntime = new WorkflowRuntime();
    //        dataExchangeService = new ExternalDataExchangeService();
      //      workflowRuntime.AddService(dataExchangeService);
            
            SqlTrackingService sqlTrackingService = new SqlTrackingService(sqlTrackingConnectionString);
            SqlWorkflowPersistenceService sqlPersistanceService = new SqlWorkflowPersistenceService(sqlPeristenceConnectionString);

            workflowRuntime.AddService(sqlTrackingService);
            workflowRuntime.AddService(sqlPersistanceService);

            workflowRuntime.StartRuntime();
        //    WorkflowRuntime runtime2 = new WorkflowRuntime();
          //  runtime2.StartRuntime();
        }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        [ClassCleanup()]
        public static void MyClassCleanup()
        {
            System.Collections.ObjectModel.ReadOnlyCollection<WorkflowInstance> workflowInstances =
             workflowRuntime.GetLoadedWorkflows();

            // workflowInstances[0].Unload();

            workflowRuntime.StopRuntime();
        }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

       
    }
}
